package com.xcm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.xcm.constant.BusinessConstant;
import com.xcm.constant.DateFormatConstant;
import com.xcm.constant.PageConstant;
import com.xcm.dao.FirmWareMapper;
import com.xcm.model.dto.FirmWareListDTO;
import com.xcm.model.vo.DeviceInfoListVO;
import com.xcm.model.vo.FirmWareListVO;
import com.xcm.page.PageInfo;
import com.xcm.service.FirmWareService;
import com.xcm.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName FirmWareServiceImpl
 * @Description 固件管理
 * @Author HanQinrui
 * @Date 2018/5/7 10:00
 * @Version 1.0
 **/
@Service("firmWareService")
public class FirmWareServiceImpl implements FirmWareService {
    @Autowired
    private FirmWareMapper firmWareMapper;
    /**
     * 获取固件分页列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<FirmWareListVO> listByPage(Integer pageNum, Integer pageSize) {
        //设置默认值
        pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
        pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        Page<FirmWareListDTO> page = firmWareMapper.listByPage();
        if (page == null) {
            return new PageInfo<>(pageNum, pageSize, 0, Lists.newArrayList());
        }
        //把Page转为PageInfo
        PageInfo<FirmWareListVO> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(page.getTotal());
        // 把DTO转为VO，并把时间戳转为字符串
        List<FirmWareListDTO> firmWareListDTOList = page.getResult();
        List<FirmWareListVO> firmWareListVOList = Lists.newArrayList();
        for (FirmWareListDTO firmWareListDTO : firmWareListDTOList) {
            FirmWareListVO firmWareListVO = new FirmWareListVO();
            firmWareListVO.setDeviceUpgradeId(firmWareListDTO.getDeviceUpgradeId());
            firmWareListVO.setName(firmWareListDTO.getName());
            firmWareListVO.setActive(firmWareListDTO.getActive());
            firmWareListVO.setModel(firmWareListDTO.getModel());
            firmWareListVO.setSize(firmWareListDTO.getSize());
            firmWareListVO.setSoftwareVersionDescription(firmWareListDTO.getSoftwareVersionDescription());
            firmWareListVO.setUpgradeType(firmWareListDTO.getUpgradeType());
            firmWareListVO.setUrl(firmWareListDTO.getUrl());
            firmWareListVO.setVersionCode(firmWareListDTO.getVersionCode());
            Long updateTime = firmWareListDTO.getUpdateTime();
            if(updateTime == null) {
                firmWareListVO.setUpdateTime("");
            } else {
                firmWareListVO.setUpdateTime(DateUtils.longTimeStampToStringDate(updateTime, DateFormatConstant.LONG_DATE_FORMAT_STR));
            }
            firmWareListVOList.add(firmWareListVO);
        }
        pageInfo.setList(firmWareListVOList);
        return pageInfo;
    }

    /**
     * 获取更新固件下终端列表
     * @param sysVersion 固件版本号
     * @param deviceStatus 在线状态 0.在线; 1.离线
     * @param deviceModel 机型
     * @param groupCode 分组编码
     * @param province 省
     * @param city 市
     * @param area 区
     * @param deviceUpgradeId 固件ID
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<DeviceInfoListVO> updateFirmWareListByPage(String sysVersion, String deviceStatus, String deviceModel, String groupCode,
                                                               String province, String city, String area, String deviceUpgradeId,
                                                               Integer pageNum, Integer pageSize) {
        //设置默认值
        pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
        pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceInfoListVO> page = firmWareMapper.updateFirmWareListByPage(sysVersion, deviceStatus, deviceModel, groupCode, province, city, area, deviceUpgradeId);
        if(page == null ) {
            return new PageInfo<>(pageNum, pageSize, 0, Lists.newArrayList());
        }
        PageInfo<DeviceInfoListVO> pageInfo = new PageInfo<>();
        pageInfo.setList(page.getResult());
        pageInfo.setPageNum(page.getPageNum());
        pageInfo.setPageSize(page.getPageSize());
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    /**
     * 查询所有固件版本号
     * @return
     */
    @Override
    public List<String> listAllFirmWareVersionCode() {
        return firmWareMapper.listAllFirmWareVersionCode();
    }

    /**
     * 根据查询条件获取在线终端总数
     *
     * @param sysVersion  固件版本号
     * @param deviceModel 机型
     * @param groupCode   分组编码
     * @param province    省
     * @param city        市
     * @param area        区
     * @return
     */
    @Override
    public Long getFirmWareDeviceOnlineCount(String sysVersion, String deviceModel, String groupCode, String province, String city, String area) {
        return firmWareMapper.getDeviceOnlineCount(BusinessConstant.DEVICE_INFO_STATUS_ON_LINE, sysVersion, deviceModel, groupCode, province, city, area);
    }

    /**
     * 根据查询条件获取离线终端总数
     *
     * @param sysVersion  固件版本号
     * @param deviceModel 机型
     * @param groupCode   分组编码
     * @param province    省
     * @param city        市
     * @param area        区
     * @return
     */
    @Override
    public Long getFirmWareDeviceOfflineCount(String sysVersion, String deviceModel, String groupCode, String province, String city, String area) {
        return firmWareMapper.getDeviceOnlineCount(BusinessConstant.DEVICE_INFO_STATUS_OFF_LINE, sysVersion, deviceModel, groupCode, province, city, area);
    }
}
